package com.xj.openapi4j.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xj.openapi4j.service.token.YsToken;
import com.xj.openapi4j.util.HttpUtil;
import com.xj.util.Constance;
import com.xj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class NoteService {
    @Autowired
    YsToken ysToken;
    @Autowired
    RedisTemplate redisTemplate;

    public String getYhtUsridByname(String name) throws Exception {
        JSONObject user =(JSONObject) redisTemplate.opsForHash().get(Constance.user, name);
        if (user != null) {
            String yhtUserid = user.getString("userId");
            if (StringUtils.isNotEmpty(yhtUserid)) {
                return yhtUserid;
            }
        }

        String token = ysToken.getToken();
        String requestUrl = "https://c3.yonyoucloud.com/iuap-api-gateway/yonbip/uspace/users/user_page_list?access_token=" + token;
        JSONObject map = new JSONObject();
        map.put("index", 0);
        map.put("size", 800);
        //   map.put("sortType","");
        String str1 = HttpUtil.post(requestUrl, JSONObject.toJSONString(map));
        log.info(str1);
        JSONObject jsonObject = JSONObject.parseObject(str1);
        if ("200".equals(jsonObject.getString("code"))) {
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray content = data.getJSONArray("content");
            for (int i = 0; i < content.size(); i++) {
                JSONObject json = content.getJSONObject(i);
                String username = json.getString("userName");
                redisTemplate.opsForHash().put(Constance.user, username, json);
            }
        }
         user = (JSONObject) redisTemplate.opsForHash().get(Constance.user, name);
        if(user!=null){
            String   yhtUserid = user.getString("userId");
            if (StringUtils.isNotEmpty(yhtUserid)) {
                return yhtUserid;
            }
        }
        //刘阳帐号
        return "8a5177ce-37b5-42b7-bd05-6c0f3b728a5a";
    }

    private final static String appId = "KqXYyZzyxk9322306249";

    public void sendMessage(String title, String content, String yhtUserid) throws Exception {
        String token = ysToken.getToken();
        String requestUrl = "https://c3.yonyoucloud.com/iuap-api-auth/yonbip/uspace/notify/share?access_token=" + token;
        JSONObject map = new JSONObject();
        map.put("appId", appId);
        map.put("title", title);
        map.put("content", content);
        //   map.put("srcMsgId", (new Date()).getTime());
        List<String> userlist = new ArrayList<>();
        userlist.add(yhtUserid);
        userlist.add("uspace_2897918");
        map.put("yhtUserIds", userlist);
        map.put("sendScope", "list");
        String str1 = HttpUtil.post(requestUrl, JSONObject.toJSONString(map));
        log.info(str1);
        JSONObject jsonObject = JSONObject.parseObject(str1);
        if (jsonObject.getIntValue("code") == 200) {
        }

    }
}


